package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.ArticleDetailVo;
import com.huan.VO.LinkVo;
import com.huan.VO.PageVo;
import com.huan.constant.SystemConstants;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Article;
import com.huan.domain.entity.Link;
import com.huan.mapper.LinkMapper;
import com.huan.service.LinkService;
import com.huan.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-06-07 09:15:20
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Autowired
    private LinkMapper linkMapper;

    @Override
    public ResponseResult getAllLink() {
//        单表查询所需结果
//        状态必须已经发布才可以返回数据
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getStatus, SystemConstants.ARTICLE_STATUS_DRAFT);
        List<Link> list = list(wrapper);
        List<LinkVo> linkVos = BeanCopyUtil.resultAllVo(list, LinkVo.class);
        ResponseResult result = ResponseResult.okResult(linkVos);
        return result;
    }

    @Override
    public ResponseResult getAllList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Link::getName,name);
        wrapper.in(Link::getStatus,status);
        Page<Link> page = new Page<>();
        page.setPages(pageNum);
        page.setSize(pageSize);
        List<Link> list = page(page, wrapper).getRecords();
        List<LinkVo> detailVos = BeanCopyUtil.resultAllVo(list, LinkVo.class);
        PageVo pageVo = new PageVo(detailVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult add(Link linkVo) {
        save(linkVo);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLink(Long id) {
        Link link = getById(id);
        LinkVo linkVo = BeanCopyUtil.resultVO(link, LinkVo.class);

        return ResponseResult.okResult(linkVo);
    }

    @Override
    public ResponseResult uplink(Link link) {
        updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(Long id) {
        linkMapper.deleteById(id);
        return ResponseResult.okResult();
    }
}






















